//
//  FXTipView.m
//  Fetion
//
//  Created by 邹天矢 on 11-4-21.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "FXTipView.h"
#import <QuartzCore/QuartzCore.h>

@implementation FXTipView

@synthesize activityIndicator = _activityIndicator;
@synthesize imageView = _imageView;
@synthesize textLabel = _textLabel;

- (id)init {
	
	CGRect tipFrame = CGRectMake(0, 0, 200, 95);
    self = [super initWithFrame:tipFrame];
	if (self == nil)
		return nil;
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.layer.cornerRadius = 10.0;
        self.layer.masksToBounds = YES;
        self.userInteractionEnabled = NO;
        self.exclusiveTouch = YES;
        CGRect rc = CGRectMake(60, 0, 200, 95);
        rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 120.0;
        self.frame = rc;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
    [_activityIndicator release];
    [_imageView release];
    [_textLabel release];
}

- (void) hide{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDidStopSelector:@selector(removeTipView)];
    self.alpha = 0.0;
    [UIView commitAnimations];
}

- (void) removeTipView{
    [self removeFromSuperview];
    if ([self retainCount] > 0) {
        [self release];
    }
}

+ (void) showTipViewWithText:(NSString *)text andImage:(UIImage *)image atCenter:(BOOL)atCenter {
    FXTipView * tipView = [[[[NSBundle mainBundle] loadNibNamed:@"TipView" owner:nil options:nil] lastObject] retain];
    tipView.textLabel.text = text;
    tipView.imageView.image = image;
    tipView.activityIndicator.hidden = YES;
    tipView.alpha = 0.0;
    
    tipView.layer.cornerRadius = 10.0;
    tipView.layer.masksToBounds = YES;
    tipView.userInteractionEnabled = NO;
    tipView.exclusiveTouch = YES;
    CGRect rc = CGRectMake(60, 0, 200, 95);
    if (atCenter) {
        rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 50.0;
    }
    else {
        rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 120.0;
    }
    tipView.frame = rc;
    
    [[UIApplication sharedApplication].keyWindow addSubview:tipView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    tipView.alpha = 0.8;
    [UIView commitAnimations];
    [tipView performSelector:@selector(hide) withObject:tipView afterDelay:2];
}

+ (void) showTipViewWithText:(NSString *)text {
    FXTipView * tipView = [[[[NSBundle mainBundle] loadNibNamed:@"TextTipView" owner:nil options:nil] lastObject] retain];
    tipView.textLabel.text = text;
    tipView.alpha = 0.0;
    
    tipView.layer.cornerRadius = 10.0;
    tipView.layer.masksToBounds = YES;
    tipView.userInteractionEnabled = NO;
    tipView.exclusiveTouch = YES;
    CGRect rc = CGRectMake(10, 0, 300, 40);
    rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 20-40;
    tipView.frame = rc;
    
    [[UIApplication sharedApplication].keyWindow addSubview:tipView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    tipView.alpha = 0.8;
    [UIView commitAnimations];
    [tipView performSelector:@selector(hide) withObject:tipView afterDelay:2];
}

+ (void) showTipViewWithTextForCloud:(NSString *)text {
    FXTipView * tipView = [[[[NSBundle mainBundle] loadNibNamed:@"TextTipView" owner:nil options:nil] lastObject] retain];
    tipView.textLabel.text = text;
    tipView.textLabel.numberOfLines=0;
    tipView.textLabel.textAlignment=UITextAlignmentLeft;
    tipView.alpha = 0.0;
    CGRect rect=tipView.textLabel.frame;
    rect.origin.y=0;
    rect.size.height=100;
    rect.origin.x=10;
    tipView.textLabel.frame=rect;
    
    tipView.layer.cornerRadius = 10.0;
    tipView.layer.masksToBounds = YES;
    tipView.userInteractionEnabled = NO;
    tipView.exclusiveTouch = YES;
    CGRect rc = CGRectMake(10, 0, 300, 100);
    rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 50.0;
    tipView.frame = rc;
    
    [[UIApplication sharedApplication].keyWindow addSubview:tipView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    tipView.alpha = 0.8;
    [UIView commitAnimations];
    [tipView performSelector:@selector(hide) withObject:tipView afterDelay:2];
}


+ (FXTipView *) showWaitTipWithTextAndIndicatorNoRespondToTouch:(NSString *)text {
    FXTipView * tipView = [[[[NSBundle mainBundle] loadNibNamed:@"TipView" owner:nil options:nil] lastObject] retain];
    tipView.textLabel.text = text;
    tipView.alpha = 0.0;
    tipView.userInteractionEnabled = YES;
    tipView.exclusiveTouch = NO;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(60, 170, 200, 95)];
    view.layer.cornerRadius = 10.0;
    view.layer.masksToBounds = YES;
    view.alpha = 1.0;
    [view setBackgroundColor:[UIColor blackColor]];
    [tipView addSubview:view];
    [view release];
    tipView.activityIndicator.frame = CGRectMake(140, 180, 37, 37);
    tipView.imageView.frame = CGRectMake(60, 200, 200, 95);
    tipView.textLabel.frame = CGRectMake(60, 200, 200, 95);;
    CGRect rc = CGRectMake(0, 0, 320, 480);
    //rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 80.0;
    tipView.frame = rc;
    [[UIApplication sharedApplication].keyWindow addSubview:tipView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    tipView.alpha = 0.6;
    [UIView commitAnimations];
    return tipView;
}

+ (FXTipView *) showWaitTipWithTextAndIndicator:(NSString *)text {
    FXTipView * tipView = [[[[NSBundle mainBundle] loadNibNamed:@"TipView" owner:nil options:nil] lastObject] retain];
    tipView.textLabel.text = text;
    tipView.alpha = 0.0;
    
    tipView.layer.cornerRadius = 10.0;
    tipView.layer.masksToBounds = YES;
    tipView.userInteractionEnabled = NO;
    tipView.exclusiveTouch = YES;
    CGRect rc = CGRectMake(60, 0, 200, 95);
    rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 120.0;
    tipView.frame = rc;

    [[UIApplication sharedApplication].keyWindow addSubview:tipView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    tipView.alpha = 0.8;
    [UIView commitAnimations];
    return tipView;
}

+ (FXTipView *) showWaitTipWithText:(NSString *)text{
    FXTipView * tipView = [[[[NSBundle mainBundle] loadNibNamed:@"TextTipView" owner:nil options:nil] lastObject] retain];
    tipView.textLabel.text = text;
    tipView.alpha = 0.0;
    
    tipView.layer.cornerRadius = 10.0;
    tipView.layer.masksToBounds = YES;
    tipView.userInteractionEnabled = NO;
    tipView.exclusiveTouch = YES;
    CGRect rc = CGRectMake(10, 0, 300, 40);
    rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 20.0;
    tipView.frame = rc;
    
    [[UIApplication sharedApplication].keyWindow addSubview:tipView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    tipView.alpha = 0.8;
    [UIView commitAnimations];
    return tipView;
}
+ (void) showTipViewWithLongText:(NSString *)text {
    FXTipView * tipView = [[[[NSBundle mainBundle] loadNibNamed:@"LongTextTipView" owner:nil options:nil] lastObject] retain];
    tipView.textLabel.text = text;
    tipView.alpha = 0.0;
    
    tipView.layer.cornerRadius = 10.0;
    tipView.layer.masksToBounds = YES;
    tipView.userInteractionEnabled = NO;
    tipView.exclusiveTouch = YES;
    CGRect rc = CGRectMake(60, 0, 200, 80);
    rc.origin.y = [UIApplication sharedApplication].keyWindow.center.y - 100.0;
    tipView.frame = rc;
    
    [[UIApplication sharedApplication].keyWindow addSubview:tipView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    tipView.alpha = 0.8;
    [UIView commitAnimations];
    [tipView performSelector:@selector(hide) withObject:tipView afterDelay:3];
}
@end
